package com.example.myworkers.service.imp;

import com.example.myworkers.dao.ICar;
import com.example.myworkers.dao.IUser;
import com.example.myworkers.model.Car;
import com.example.myworkers.model.User;
import com.example.myworkers.service.CarService;
import com.example.myworkers.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IUserServiceImpl implements IUserService {
    @Autowired
   private IUser userMapper;

    @Override
    public int insertUser(User user){
        int userValue=0;
        User user1=new User();
        user1.setUser_name(user.getUser_name());
        List<User> getU=userMapper.getAllUser(user1);
        if(getU.size()>=1){
            userValue=-1;
        }else {
            userValue=userMapper.insertUser(user);
        }
        return userValue;
    }

    public List<User> getAllUser(User user){
        return userMapper.getAllUser(user);
    }

    @Override
    public int updateUser(User user){
        int userValue=0;
        User user2=new User();
        user2.setUser_name(user.getUser_name());
        if(user.getUser_id()!=0){
            if(user.getUser_id()>=1){
                if(user2.getUser_name()!=user.getUser_name()){
                    userValue=-1;
                }
            }
            userValue=userMapper.updateUser(user);
        }
        return userValue;
    }

    @Override
    public int deleteUser(User user){
        int userValue=0;
        if(user.getUser_id()!=0){
            userValue=userMapper.deleteUser(user);
        }
        return userValue;
    }

    @Override
    public User getUser(User user){
       User user1=new User();
        if(user.getUser_id()!=0){
            user1=userMapper.getUser(user);
        }
        return user1;
    }


}
